package com.ajouroid.smartcoworker;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Slide {

	ArrayList<String> files;
	Bitmap[] pages;
	String team_id;
	boolean down = false;

	public Slide(String t_id, ArrayList<String> f) {
		team_id = t_id;
		files = f;
		pages = new Bitmap[f.size()];
	}

	public String get(int i) {
		return files.get(i);
	}

	public int size() {
		return files.size();
	}

	public void setPage(Context context, CustomImageView v, ProgressBar p, int index) {
		ProgressBar pro;
		pro = p;
		if (pages[index] == null) {
			
				
			String url = HttpConnector.getAddress() + team_id + "/"
					+ files.get(index);
			LoadImageTask task = new LoadImageTask(context, v,pro, index);
			task.execute(url);
			
		} 
		else
		{
			v.setImageBitmap(pages[index], index);
		}
			
		if(index>6)
			{
				for(int i=0;i<index-3;i++)
				{
					pages[i] = null;
				}
			}
			
		if(index+3 < files.size())
			{
			
			
			if(pages[index+3] != null)
			{
				for(int i=index+3;i<files.size();i++)
				{
					pages[i] = null;
				}
			}
			}
			
		
	}
	

	public void downall(final Context context,int position,ImageView img,TextView txt1,LinearLayout c,LinearLayout d)//,ProgressBar pro2)
	{
		
		
		if(position <files.size())
		{
					
			if(pages[position] != null)
			{
				
				img.setImageBitmap(pages[position]);
				img.setTag(position);
				txt1.setText("-"+(position+1)+"-");
				img.setOnTouchListener(new OnTouchListener(){

					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						
						((MainActivity)context).a = (Integer)v.getTag();
							
						return false;
					}
					
				});			
			}
		
			else
			{
			listImageTask task = new listImageTask(context,position,img,txt1,c,d);//,pro2);
			task.execute();
			}
		}
	}
		
	

	public void loadImage() {

	}

	private class LoadImageTask extends AsyncTask<String, Bitmap, Bitmap> {
		int page = 0;
		CustomImageView view = null;
		ProgressBar prog;

		Context mCtx;

		public LoadImageTask(Context context, CustomImageView v, ProgressBar pro,int p) {
			view = v;
			prog = pro;
			page = p;
			mCtx = context;
		}

		@Override
		protected Bitmap doInBackground(String... url) {
			Bitmap bm = null;

			try {
				URL u = new URL(url[0]);

				URLConnection uc = u.openConnection();

				uc.connect();

				BufferedInputStream bis = new BufferedInputStream(
						uc.getInputStream());

				bm = BitmapFactory.decodeStream(bis);

				pages[page] = bm;

				bis.close();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bm;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			prog.setVisibility(View.GONE);
			view.setVisibility(View.VISIBLE);
			if (result != null && view != null) {
				view.setImageBitmap(result, page);
				view.tt = 0;
				view.tuningMatrix(view.matrix, view, false);
				view.setImageMatrix(view.matrix);
				view.setImagePit();
				view.matrix.getValues(view.matrixValue);
				view.orginScale = view.matrixValue[0];
				//dialog.dismiss();
			}

			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			view.setVisibility(View.GONE);
			prog.setVisibility(View.VISIBLE);

			if (view != null) {

			}
			super.onPreExecute();
		}
	}

	private class listImageTask extends AsyncTask<Void, Void, Void>
	{
		int page = 0;
		int posi=0;
		ImageView img1=null;
		TextView txt11;
		Context mCtx ;
		LinearLayout z;
		LinearLayout w;
		String url;
		public listImageTask(Context context,int position,ImageView img,TextView txt1,LinearLayout e,LinearLayout f)//,ProgressBar pro2)
		{	
			
			mCtx = context;
			posi = position;
			img1 = img;
			txt11 = txt1;
			z = e;
			w = f;
			//ppro2 = pro2;
			
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			Bitmap bm = null;
			
			
				if(posi < files.size())
				{
				if(pages[posi] == null)
				{
				url = HttpConnector.getAddress() + team_id + "/" + files.get(posi);
			try {				
				URL u = new URL(url);
				
				URLConnection uc = u.openConnection();
					
				uc.connect();
					
				BufferedInputStream bis = new BufferedInputStream(uc.getInputStream());
				
					
				bm = BitmapFactory.decodeStream(bis);
				
				pages[posi] = bm;
				
				bis.close();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			
				}
			return null;
		}

		@Override
		protected void onPostExecute(Void unused) {			
			
			txt11.setText("-"+(posi+1)+"-");
			
			z.setVisibility(View.INVISIBLE);
			w.setVisibility(View.VISIBLE);
			//ppro2.setVisibility(View.GONE);
			//imgg2.setVisibility(View.VISIBLE);
			
			
				if(posi < files.size())
				{
					
				img1.setImageBitmap(pages[posi]);
				img1.setTag(posi);
				img1.setOnTouchListener(new OnTouchListener(){

					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						
						((MainActivity)mCtx).a = (Integer)v.getTag();
							
						return false;
					}
					
				});
				}
						
		}
		@Override
		protected void onPreExecute() {
			w.setVisibility(View.INVISIBLE);
			img1.setImageBitmap(pages[0]);
			z.setVisibility(View.VISIBLE);
			
			//imgg2.setVisibility(View.GONE);
			//ppro2.setVisibility(View.VISIBLE);
			
				
			super.onPreExecute();
		}


		
	}

}
